package cn.com.sealevel.main;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import cn.com.sealevel.client.Terminal;
import cn.com.sealevel.util.ConfigUtil;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/* loaded from: netty-mqtt-client-1.0.0.jar:cn/com/sealevel/main/Main.class */
public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private List<Terminal> terminals;
    private long startMillis;
    private long stopMillis;
    public static CountDownLatch latch;
    private EventLoopGroup workerGroup = null;
    private int randomTerminalIndex = 0;

    public static void main(String[] args) {
        MDC.put("role", "hnxb-benchmark");
        Main main = new Main();
        main.startLog();
        main.init();
        main.start();
        main.stop();
    }

    private void init() {
        new ConfigUtil().parse();
        this.workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2);
        latch = new CountDownLatch(ConfigUtil.terminalNum);
        this.terminals = new ArrayList<>(ConfigUtil.terminalNum);
        this.randomTerminalIndex = new Random().nextInt(ConfigUtil.terminalNum);
    }

    private void start() {
        Random random = new Random(System.currentTimeMillis());
        this.startMillis = System.currentTimeMillis();
        long baseClientId = (4000000000L + ConfigUtil.startId) * 100000;
        for (int i = 0; i < ConfigUtil.terminalNum; i++) {
            String clientId = "XB" + (baseClientId + ((long) i));
            Terminal terminal = new Terminal(this.workerGroup, clientId, ConfigUtil.username, ConfigUtil.password, random.nextInt(ConfigUtil.connectDelayInMillis), ConfigUtil.sendDataCount);
            if (i == this.randomTerminalIndex) {
                logger.info("抽样车辆 终端编号[{}]", clientId);
                terminal.setNeedSave(true);
            }
            terminal.start();
            this.terminals.add(terminal);
        }
    }

    /* JADX WARN: Type inference failed for: r0v38, types: [long] */
    private void stop() {
        try {
            try {
                latch.await();
                this.stopMillis = System.currentTimeMillis();
                logger.info("All terminals work done.");
                int validNum = 0;
                long c = 0;
                for (Terminal terminal : this.terminals) {
                    if (terminal.getStatus()) {
                        validNum++;
                    }
                    c += terminal.getSentNum();
                }
                Terminal randomTerminal = this.terminals.get(this.randomTerminalIndex);
                logger.info("[END]模拟程序结束：");
                logger.info("[RESULT]测试持续时长：|{}|秒，发起连接数：|{}|，有效连接数：|{}|，发送总数据量：|{}|", (this.stopMillis - this.startMillis) / 1000, this.terminals.size(), validNum, c);
                logger.info("[SAMPLE]抽样车辆识别号：|{}|，发送数据总量：|{}|，数据文件：|{}|", randomTerminal.getClientId(), randomTerminal.getSentNum(), randomTerminal.getFilePath());
                logger.info("GB4Benchmark will shutdown gracefully.");
                this.workerGroup.shutdownGracefully();
            } catch (InterruptedException e) {
                logger.error("InterruptedException", e);
                logger.info("GB4Benchmark will shutdown gracefully.");
                this.workerGroup.shutdownGracefully();
            }
        } catch (Throwable th) {
            logger.info("GB4Benchmark will shutdown gracefully.");
            this.workerGroup.shutdownGracefully();
            throw th;
        }
    }

    private void startLog() {
        try {
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            loggerContext.reset();
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(loggerContext);

            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("logback.xml");
            configurator.doConfigure(inputStream);
            logger.info("启动日志记录器成功");
        } catch (Exception e) {
            System.out.println("启动日志记录器时发生错误:" + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }
}
